<?php
######################################################################################
# XC SEO :: http://code.google.com/p/x-cart-seo/
# Description: This is an SEO optimizing module for the X-Cart shopping cart
# Author: PHP Site Solutions (intel352 aka Jon Langevin)
# Website: http://www.phpsitesolutions.com
# License: http://www.gnu.org/licenses/gpl.html GPL version 3 or any later version
#######################################################################################

if ( !defined('XCART_START') ) { header('Location: ../'); die('Access denied'); }

$cdseo_array = array();
if($config['XC_SEO']['xcseo_migrate_cdseo_cat'] != '')
	$cdseo_array[] = array($xcart_web_dir . '/' . $config['XC_SEO']['xcseo_migrate_cdseo_cat'], 'cat');
if($config['XC_SEO']['xcseo_migrate_cdseo_prod'] != '')
	$cdseo_array[] = array($xcart_web_dir . '/' . $config['XC_SEO']['xcseo_migrate_cdseo_prod'], 'productid');
if($config['XC_SEO']['xcseo_migrate_cdseo_pages'] != '')
	$cdseo_array[] = array($xcart_web_dir . '/' . $config['XC_SEO']['xcseo_migrate_cdseo_pages'], 'pageid');
if($config['XC_SEO']['xcseo_migrate_cdseo_manu'] != '')
	$cdseo_array[] = array($xcart_web_dir . '/' . $config['XC_SEO']['xcseo_migrate_cdseo_manu'], 'manufacturerid');

if($cdseo_array) {
	foreach($cdseo_array AS $v) {
		if(	($strpos = strpos($seo_path, $v[0]))!==false ) {
			$cdseo_file = substr($seo_path, $strpos + strlen($v[0]));
			$cdseo_file = ltrim( rtrim($cdseo_file, '/'), '/');

			if(class_exists('seo_filter')) {
				$seo = new seo_filter;

				$seo_qs = array();
				foreach($_GET AS $kk=>$vv){
					if($kk!=$XCART_SESSION_NAME)
						$seo_qs[] = $kk.'='.$vv;
				}

				switch($v[1]) {
					case 'cat':
						$id = func_query_first_cell('SELECT categoryid FROM ' . $sql_tbl['categories'] . ' WHERE cdseo_url!="" AND cdseo_url="' . mysql_real_escape_string($cdseo_file) . '"');
						$callback = '_category_callback';
					break;
					case 'productid':
						$id = func_query_first_cell('SELECT productid FROM ' . $sql_tbl['products'] . ' WHERE cdseo_url!="" AND cdseo_url="' . mysql_real_escape_string($cdseo_file) . '"');
						$callback = '_product_callback';
					break;
					case 'pageid':
						$id = func_query_first_cell('SELECT pageid FROM ' . $sql_tbl['pages'] . ' WHERE filename="' . mysql_real_escape_string($cdseo_file) . '"');
						$callback = '_pages_callback';
					break;
					case 'manufacturerid':
						$id = func_query_first_cell('SELECT manufacturerid FROM ' . $sql_tbl['manufacturers'] . ' WHERE cdseo_url!="" AND cdseo_url="' . mysql_real_escape_string($cdseo_file) . '"');
						$callback = '_manufacturer_callback';
					break;
				}
				$seo_qs[] = $v[1].'='.$id;
				$seo_qs = array(false,'','',implode('&',$seo_qs),'','');

				$seo_url = $xcart_web_dir.'/'.$seo->$callback($seo_qs, true);

				if($seo_url && $seo_url!=$_SERVER['REQUEST_URI']){
					xcseo_redirect($seo_url);
				}
			}
		}
	}
}

?>